<?php

namespace app\api\model\group;

use app\api\model\group\Group as GroupModel;
use app\api\model\group\GroupOrder as GroupOrderModel;
use app\api\model\User;
use think\Db;
use think\Model;
use traits\model\SoftDelete;


class GroupOrder extends Model
{

    use SoftDelete;



    // 表名
    protected $name = 'group_order';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'group_status_text',
        'gender_text'
    ];





    public function getGroupStatusList()
    {
        return [0=>'待审核',1=>'审核通过',2=>'审核不通过',3=>'暂离',4=>'退团',5=>'警告',6=>'拉黑',7=>'踢出',8=>'取消',9=>'暂离'];
    }

    public function getGenderList()
    {
        return [0=>'女',1=>'男'];
    }


    public function getGroupStatusTextAttr($value, $data)
    {
        $value = $value ?: ($data['group_status'] ?? '');
        $list = $this->getGroupStatusList();
        return $list[$value] ?? '';
    }

    public function getGenderTextAttr($value, $data)
    {
        $value = $value ?: ($data['gender'] ?? '');
        $list = $this->getGenderList();
        return $list[$value] ?? '';
    }


    //抱团申请
    public function apply($params)
    {
        $group_id = $params['group_id'];
        $user_id = $params['user_id'];

        $group_info = GroupModel::get($group_id);
        if (!$group_info)  throw new \Exception('抱团不存在');
        if ($group_id==$group_info['user_id'])  throw new \Exception('不能报自己发布的团');

        $user_info = User::get($user_id);
        if ($user_info['level']==0){
            throw new \Exception('普通用户不能报名');
        }
        if ($group_info['user_level']!='0'){
            $user_level_arr = explode(',',$group_info['user_level']);
            if (!in_array($user_info['level'],$user_level_arr)){
                throw new \Exception('不满足该组团的服务对象要求');
            }
        }

        $order_model = new GroupOrderModel();
        $orderinfo = GroupOrderModel::get(['user_id'=>$user_id, 'group_id'=>$group_info->group_id]);

        if ($orderinfo){
            if ($orderinfo['group_status']==7){
                throw new \Exception('您已被团长拉入黑名单,不能申请抱团');
            } else {
                throw new \Exception('该抱团已申请');
            }
        }
        Db::startTrans();
        try {
            $data = [
                'user_id'=>$user_id,
                'head_id'=>$group_info->user_id,
                'group_id'=>$group_info->group_id,
                'realname'=>$params['realname'],
                /*'nickname'=>$params['nickname'],
                'realname'=>$params['realname'],
                'gender'=>$params['gender'],
                'age'=>$params['age'],
                'education'=>$params['education'],
                'major'=>$params['major'],
                'title'=>$params['title'],*/
                'mobile'=>$params['mobile'],
                /*'image'=>$params['image'],
                'province'=>$params['province'],
                'city'=>$params['city'],
                'district'=>$params['district'],
                'province_id'=>$params['province_id'],
                'city_id'=>$params['city_id'],
                'district_id'=>$params['district_id'],*/
                //'self_intro'=>$params['self_intro'],
                //'reg_reasons'=>$params['reg_reasons'],
            ];
            /*if (empty($data['nickname'])){
                throw new \Exception('请输入昵称');
            }

            if (empty($data['age'])){
                throw new \Exception('请输入年龄');
            }
            if (empty($data['education'])){
                throw new \Exception('请输入学历');
            }
            if (empty($data['major'])){
                throw new \Exception('请输入专业');
            }
            if (empty($data['title'])){
                throw new \Exception('请输入职称');
            }*/
            if (empty($data['realname'])){
                throw new \Exception('请输入申请人');
            }
            if (empty($data['mobile'])){
                throw new \Exception('请输入电话');
            }
            /*if (empty($data['province'])||empty($data['city'])||empty($data['district'])){
                throw new \Exception('请选择区域');
            }*/
            $res = $order_model->save($data);

            Db::commit();
            return true;
        }catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw new \Exception($e->getMessage().'-'.$e->getLine());
        }


    }

    //修改抱团申请
    public function edit($params)
    {
        //$group_id = $params['group_id'];
        $user_id = $params['user_id'];
        $order_id = $params['order_id'];

        //$group_info = GroupModel::get($group_id);
        //if (!$group_info)  throw new \Exception('抱团不存在');

        $order_model = new GroupOrderModel();
        //$orderinfo = GroupOrderModel::get(['user_id'=>$user_id, 'group_id'=>$group_info->group_id]);
        //if ($orderinfo)throw new \Exception('该抱团已申请');
        Db::startTrans();
        try {
            $data = [
                'user_id'=>$user_id,
                //'head_id'=>$group_info->user_id,
                //'group_id'=>$group_info->group_id,
                //'nickname'=>$params['nickname'],
                'realname'=>$params['realname'],
                /*'gender'=>$params['gender'],
                'age'=>$params['age'],
                'education'=>$params['education'],
                'major'=>$params['major'],
                'title'=>$params['title'],*/
                'mobile'=>$params['mobile'],
                /*'image'=>$params['image'],
                'province'=>$params['province'],
                'city'=>$params['city'],
                'district'=>$params['district'],
                'province_id'=>$params['province_id'],
                'city_id'=>$params['city_id'],
                'district_id'=>$params['district_id'],
                'self_intro'=>$params['self_intro'],
                'reg_reasons'=>$params['reg_reasons'],*/
                'group_status'=>0,
                //'refuse_reasons'=>''
            ];
            /*if (empty($data['nickname'])){
                throw new \Exception('请输入昵称');
            }*/
            if (empty($data['realname'])){
                throw new \Exception('请输入姓名');
            }
            /*if (empty($data['age'])){
                throw new \Exception('请输入年龄');
            }
            if (empty($data['education'])){
                throw new \Exception('请输入学历');
            }
            if (empty($data['major'])){
                throw new \Exception('请输入专业');
            }
            if (empty($data['title'])){
                throw new \Exception('请输入职称');
            }*/

            if (empty($data['mobile'])){
                throw new \Exception('请输入电话');
            }
            /*if (empty($data['province'])||empty($data['city'])||empty($data['district'])){
                throw new \Exception('请选择区域');
            }*/
            $res = $order_model->save($data,['order_id'=>$order_id]);

            Db::commit();
            return true;
        }catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw new \Exception($e->getMessage().'-'.$e->getLine());
        }


    }

}
